package com.example.cashier

import android.os.Bundle
import android.text.Editable
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.cashier.databinding.ActivityCashierAddBinding
import com.example.cashier.entity.CashierAddParam
import com.example.cashier.vm.CashierViewModel
import com.example.common.view.view.BaseMvvmActivity
import com.github.gzuliyujiang.wheelpicker.DatePicker
import com.github.gzuliyujiang.wheelpicker.OptionPicker
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat

@AndroidEntryPoint
@Route(path="/cashier/CashierAddActivity")
class CashierAddActivity : BaseMvvmActivity<ActivityCashierAddBinding>() {
    override fun getLayoutId(): Int =R.layout.activity_cashier_add
    val cashierViewModel:CashierViewModel by viewModels ()
    override fun initView() {
        super.initView()
        mDataBinding.ivBack.setOnClickListener {
            finish()
        }
        mDataBinding.tvAddress.text= SPUtils.getInstance().getString("gaode").toString()
        //类型选择器
        mDataBinding.ivChargeType.setOnClickListener {
            val list = mutableListOf("物业费","水电费","停车费","住宿费")
            val optionPiker= OptionPicker(this)
            optionPiker.setData(list)
            optionPiker.setTitle("请选择收费项目")
            optionPiker.setOnOptionPickedListener { position, item ->
                mDataBinding.edChargeType.text= Editable.Factory.getInstance().newEditable(list[position])
            }
            optionPiker.show()
        }
        //开始时间
        mDataBinding.ivEffectiveDate.setOnClickListener {
            val dataPicker= DatePicker(this)
            dataPicker.setTitle("请选择开始日期")
            dataPicker.wheelLayout.setRange(
                DateEntity.target(1900,1,1), DateEntity.target(2600,12,31),
                DateEntity.today())
            dataPicker.setOnDatePickedListener { year, month, day ->
                mDataBinding.edEffectiveDate.text=Editable.Factory.getInstance().newEditable("$year-$month-$day")
            }
            dataPicker.show()
        }
        //结束时间
        mDataBinding.ivExpirationDate.setOnClickListener {
            val dataPicker= DatePicker(this)
            dataPicker.setTitle("请选择结束日期")
            dataPicker.wheelLayout.setRange(
                DateEntity.target(1900,1,1), DateEntity.target(2600,12,31),
                DateEntity.today())
            dataPicker.setOnDatePickedListener { year, month, day ->
                mDataBinding.edExpirationDate.text=Editable.Factory.getInstance().newEditable("$year-$month-$day")
            }
            dataPicker.show()
        }
        mDataBinding.btnCommit.setOnClickListener {
            var cashierAddParam=CashierAddParam(
                actualAmount=1200,
             balanceDeduction=1200,
             chargeAmount=1200,
             chargeType=mDataBinding.edChargeType.text.toString(),
             contactNumber=13955667788,
             discountAmount=0,
             effectiveDate=mDataBinding.edEffectiveDate.text.toString(),
             expirationDate=mDataBinding.edExpirationDate.text.toString(),
             id=0,
             operator="哈基米",
             payerId=4,
             payerName="哈基米",
             paymentMethod="支付宝",
             paymentStatus= "未缴费",
             paymentTime= SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis()),
             penalty=0,
             receivableAmount=1200
            )
            cashierViewModel.addCashier(cashierAddParam)
        }
        cashierViewModel.mSuccessLiveData.observe(this){
            ToastUtils.showLong("添加成功")
            finish()
        }
    }

}